// 蜂窝蜜造平台生成代码，此文件只生成一次，不需要添加到 .beeignore

package com.fowo.api.service.impl;

import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.fowo.api.entity.QualityCheckStorageBadInfo;
import com.fowo.api.model.quality.check.storage.bad.info.QualityCheckStorageBadInfoItemVo;
import com.fowo.api.model.quality.check.storage.bad.info.QualityCheckStorageBadInfoVo;
import com.fowo.api.service.QualityCheckStorageBadInfoService;
import com.fowo.api.sys.service.FileService;
import com.fowo.api.sys.service.SysImportTemplateService;
import com.fowo.api.user.model.JwtUserInfo;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import javax.validation.Validator;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * 质检次品信息 服务实现类
 */
@Service
public class QualityCheckStorageBadInfoServiceImpl
  extends QualityCheckStorageBadInfoBaseServiceImpl implements QualityCheckStorageBadInfoService {



    @Resource
    protected FileService fileService;

    @Resource
    protected SysImportTemplateService importTemplateService;


    @Resource
    protected Validator validator;


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean create(List<QualityCheckStorageBadInfoVo> list) throws Exception {
        if (CollUtil.isNotEmpty(list)) {
            //01 删除冗余数据
            this.remove(Wrappers.<QualityCheckStorageBadInfo>lambdaQuery().in(QualityCheckStorageBadInfo::getParentId, CollUtil.getFieldValues(list, "parentId")));
            JwtUserInfo currentUser = JwtUserInfo.fromHeader();
            if (CollUtil.isEmpty(list)) {
                throw new Exception("参数异常!");
            }
            //02 每一种不良类型保存一条记录
            List<QualityCheckStorageBadInfo> entityList = new ArrayList<>();
            for (QualityCheckStorageBadInfoVo qcModel : list) {
                if (CollUtil.isEmpty(qcModel.getList())) {
                    continue;
                }
                for (QualityCheckStorageBadInfoItemVo model : qcModel.getList()) {
                    QualityCheckStorageBadInfo entity = new QualityCheckStorageBadInfo();
                    BeanUtils.copyProperties(model, entity);
                    entity.setClassify(model.getClassifyid());
                    entity.setParentId(qcModel.getParentId());
                    entity.setLetter(qcModel.getLetter());
                    entity.setSku(qcModel.getSku());
                    entity.setCreateTime(new Date());
                    entity.setLastUpdateTime(new Date());
                    if (currentUser != null) {
                        entity.setCreateUser(currentUser.getUserId());
                        entity.setLastUpdateUser(currentUser.getUserId());
                    }
                    entityList.add(entity);
                }
            }
            //03 批量插入
            return this.saveBatch(entityList);
        }
        throw new Exception("质检次品信息保存失败");
    }


    @Override
    public List<Map<String, Object>> listSearch(List<Long> parentId) {
        List<Map<String, Object>> maps = this.baseMapper.listSearch(parentId);
        return maps;
    }
}
